package com.j180.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.j180.bean.Customer;
import com.j180.bean.ResultBean;
import com.j180.service.CustomerServiceImpl;
import com.j180.service.ICustomerService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class ShowAllCustomerServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决乱码
         req.setCharacterEncoding("utf-8");
         resp.setCharacterEncoding("utf-8");
         resp.setContentType("text/html;charset=utf-8");

         //创建业务层
        ICustomerService iCustomerService = new CustomerServiceImpl();
        //取数据
        List<Customer> list = iCustomerService.showAllCustomersInfo();
        ResultBean resultBean =null;
        if(list.size()!=0){
            resultBean = new ResultBean(1,list,"查询成功");
        }else{
            resultBean = new ResultBean(0,list,"没有数据");
        }

       ObjectMapper objectMapper = new ObjectMapper();

        resp.getWriter().print(objectMapper.writeValueAsString(resultBean));

        }

    }

